package edu.hawaii.halealohacli.command;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * A simple application that shows basic interaction with a running WattDepot server.
 * 
 * @author Yong hong Hsu
 * @author Russell Vea
 * @author Brande Ogata
 */

public class EnergySince implements Command {
  
  private double energy = 0.0;
  
  /**
   * Calculates and prints the results of the energy-since command.
   * 
   * @param client a WattDepot client
   * @param source the source to query
   * @param days the first being the timestamp for the day to start at
   * @throws Exception on an error with the Server
   */
  @Override
  public void printResults(WattDepotClient client, Source source, 
                           XMLGregorianCalendar... days) throws Exception {

    if (client.isHealthy()) {
      System.out.format("Connected successfully to: %s%n", source.getName());
    }
    else {
      System.out.format("Could not connect to: %s%n", source.getName());
      return;
    }
    
    //make timestamp for today
    XMLGregorianCalendar end = Tstamp.makeTimestamp();
    end.setHour(0);
    end.setMinute(0);
    end.setSecond(0);
    end.setMillisecond(0);
    String name = source.getName();
    this.energy = client.getEnergyConsumed(name, days[0], end, 15) / 1000;
    System.out.format("Source: %s      %s kWh consumed since %tD %n", 
        name, this.energy , days[0].toGregorianCalendar());
  }
  
  /**  
   * Returns the value from the last daily-energy command.
   * 
   * @return this.energy for testing purposes. 
   * 
   */
  public double getEnergy() {
    return this.energy;
  }
}
